<?php

/**
 * 上级活动
 */
namespace app\api\modules\v1\market\controllers;

use yii\web\Controller;
use app\api\components\ApiController;
use Yii;
use app\models\base\User;
use app\models\base\ProjectMember;
use yii\db\Query;
use app\api\modules\v1\market\models\AppUser;
use app\api\modules\v1\market\models\Message;
use app\api\modules\v1\market\models\Member;
use app\models\base\Project;
use app\models\base\Activity;
use app\api\modules\v1\market\models\DataFormat;
use app\api\modules\v1\market\models\UpdateProject;

class SupactivityController extends ApiController
{
	/**
	 * [actionPactivity 新建子活动时显示上级活动信息]
	 * @return [type] [description]
	 */
	public function actionPactivity()
	{
		$get = Yii::$app->request->get();
		$pid = $get['pid'];
		if(!$pid){
			return parent::sendMessageJson('请选择父级活动');
		}
		$activity = Activity::findOne($pid);
		if(!$activity){
			return parent::sendMessageJson('不存在此活动');
		}
		$params = [
			'name' => $activity->name,
			'desc' => $activity->desc,
			'surplus_value' => $activity->surplus_value,
			'surplus_gold' => $activity->surplus_gold,

		];
		return parent::sendMessageJson(Message::SUCCESS,parent::$CODE_SUC,$params);
	}

	/**
	 * [actionPdetail 活动详情上属活动的信息]
	 * @return [type] [description]
	 */
	public function actionPdetail()
	{
		$get = Yii::$app->request->get();
		$activity_id = $get['activity_id'];
		if(!$activity_id){
			return parent::sendMessageJson('请选择活动');
		}
		$activity = Activity::findOne($activity_id);
		if(!$activity){
			return parent::sendMessageJson('找不到此活动');
		}
		//查找上级活动的信息
		return $pdetail = $this->parentdetail($activity);

		// var_dump($activity_id);exit;
	}

	/**
	 * [parentdetail 查找上级活动数据的函数]
	 * @param  [type] $activity [当前活动的信息]
	 * @return [type]           [上级活动的信息]
	 */
	public function parentdetail($activity)
	{
		if(empty($activity->parent_id)){
			return parent::sendMessageJson('该活动为一级活动',parent::$CODE_SUC);
		}else{
			$pactivity = Activity::findOne($activity->parent_id);
			$params = [
				'activity_id' => $pactivity->activity_id,
				'type' => $pactivity->type,
				'status' => $pactivity->status,
				'name' => $pactivity->name,
				'handler' => DataFormat::format_user($pactivity->handler_user_id),
				'create_time' => DataFormat::format_create_time($pactivity->create_time,'.'),
				'priority' => $pactivity->priority,
			];
			return parent::sendMessageJson(Message::SUCCESS,parent::$CODE_SUC,$params);
		}
	}

	/**
	 * [actionUpdateProject 项目创建者可改变项目的金币]
	 * @return [type] [成功或失败]
	 */
	public function actionUpdateProject()
	{
		$post = Yii::$app->request->post();

		$validate = \yii\base\DynamicModel::validateData(self::handler_post(['user_id','token','project_id','all_gold']),[
            [['user_id'],'required','message'=>Message::EMPTY_USER],
            [['token'],'required','message'=>'token必填'],
            [['project_id'],'required','message'=>'项目id必填'],
            [['all_gold'],'required','message'=>'金币必填'],
        ]);

        if($validate -> hasErrors()){
            return self::sendMessageJson(array_values($validate->getFirstErrors())[0]);
            exit;
        }
		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($post['user_id'],$post['token']) == false) return parent::sendMessageJson('用户认证不通过');

        //验证是否存在项目
        $project = Project::findOne($post['project_id']);
        if(!$project){
        	return parent::sendMessageJson('不存在此活动');
        }

        //修改项目的金币
        if($project->create_user_id == $post['user_id']){
        	return $res = UpdateProject::updategold($project,$post['all_gold']);
        }else{
        	return parent::sendMessageJson('没有权限修改金币');
        }
	}

	public static function handler_post($field,$post=false)
    {
        $post = \yii::$app->request->post();
        $arr = [];
        foreach($field as $k => $val){
            $arr[$val] = isset($post[$val])?$post[$val]:'';
        }
        return $arr;
    }
}